<?php
include("include/conn.php");
require('include/Smarty/libs/Smarty.class.php');

$partyId           = isset($_REQUEST['party']) ? $_REQUEST['party'] : 0;
$openingFinalFineDr = 0;
$openingFinalFineCr = 0;
$openingFinalAmountDr = 0;
$openingFinalAmountCr = 0;
$totalFineCr   = 0;
$totalFineDr   = 0;
$totalAmountCr = 0;
$totalAmountDr = 0;
$fromDate      = isset($_REQUEST['fromDate']) ? $_REQUEST['fromDate'] : "";
$toDate        = isset($_REQUEST['toDate']) ? $_REQUEST['toDate'] : "";
$weight        = "";
$grossWeight        = "";
$l_weightPerc        = "";
$touch         = "";
$bhatthiPartyId= "";
$party         = array();
$bhatthiBookCr = array();
$bhatthiBookDr = array();
$opening       = array();
$total         = array();

if(!isset($_REQUEST['party']) && !isset($_REQUEST['bhatthiId']))
  $partyId = 0;
else
  $partyId = isset($_REQUEST['party']) ? $_REQUEST['party'] : $_GET['bhatthiId'];

if(isset($_POST['party']) && $_POST['party'] > 0)
{
  $partyId   = $_POST['party'];
  $fromDate  = $_POST['fromDateYear']."-".$_POST['fromDateMonth']."-".$_POST['fromDateDay'];
  $toDate    = $_POST['toDateYear']."-".$_POST['toDateMonth']."-".$_POST['toDateDay'];
  
  $bhatthiBookDetail = "SELECT bhatthibookdetail.bhatthiBookDetailId, bhatthibook.bhatthiBookId, bhatthibook.bhatthiPartyId, DATE_FORMAT(bhatthibook.bhatthiDate,'%Y-%m-%d') As bhatthiDate,
                               bhatthibookdetail.particulars, party.partyName,bhatthibookdetail.grossWeight,bhatthibookdetail.weightPerc, bhatthibookdetail.weight, bhatthibookdetail.touch, bhatthibookdetail.fine, bhatthibookdetail.qty, 
                               bhatthibookdetail.rate, bhatthibookdetail.amount, bhatthibookdetail.CrDr, bhatthibook.bhatthiPartyId
                          FROM bhatthibook
                          JOIN bhatthibookdetail ON bhatthibookdetail.bhatthiBookId = bhatthibook.bhatthiBookId
                          JOIN party ON party.partyId = bhatthibook.bhatthiPartyId
                         WHERE bhatthibook.bhatthiPartyId = ".$partyId."
                           AND bhatthibook.bhatthiDate <= '".$toDate."'
                         ORDER BY bhatthibook.bhatthiDate, bhatthibookdetail.bhatthiBookDetailId, bhatthibookdetail.bhatthiBookId";
  $bhatthiBookDetailRes = mysql_query($bhatthiBookDetail);
  $drCounter           = 0;
  $crCounter           = 0;
  
  $total['drgrossWeight']= 0;
  $total['drweightPerc']= 0;
  $total['drWeight']   = 0;
  $total['drTouch']    = 0;
  $total['drFine']     = 0;
  $total['drQty']     = 0;
  $total['drAmount']   = 0;
  $total['crFine']     = 0;
  $total['crQty']      = 0;
  $total['crAmount']   = 0;
  
  $opening['crgrossWeight'] = 0; 
  $opening['crweightPerc'] = 0; 
  $opening['crWeight'] = 0; 
  $opening['drWeight'] = 0; 
  $opening['crTouch']  = 0; 
  $opening['drTouch']  = 0; 
  $opening['crFine']   = 0; 
  $opening['drFine']   = 0; 
  $opening['crQty']    = 0; 
  $opening['crRate']   = 0; 
  $opening['crAmount'] = 0; 
  $opening['drAmount'] = 0; 
  
  while($bhatthiRow = mysql_fetch_assoc($bhatthiBookDetailRes))
  {
    if($bhatthiRow['bhatthiDate'] >= $fromDate)
    {
      if($bhatthiRow['CrDr'] == 'Dr')
      {
        $bhatthiBookDr[$drCounter]['bhatthiBookDetailId'] = $bhatthiRow['bhatthiBookDetailId'];
        $bhatthiBookDr[$drCounter]['bhatthiBookId']       = $bhatthiRow['bhatthiBookId'];
        $bhatthiBookDr[$drCounter]['bhatthiPartyId']      = $bhatthiRow['bhatthiPartyId'];
        $bhatthiBookDr[$drCounter]['date']                = $bhatthiRow['bhatthiDate'];
        $bhatthiBookDr[$drCounter]['particulars']         = $bhatthiRow['particulars'];
        $bhatthiBookDr[$drCounter]['partyName']           = $bhatthiRow['partyName'];
        $bhatthiBookDr[$drCounter]['grossWeight']         = $bhatthiRow['grossWeight'];
        $bhatthiBookDr[$drCounter]['weightPerc']          = $bhatthiRow['weightPerc'];
        $bhatthiBookDr[$drCounter]['weight']              = $bhatthiRow['weight'];
        $bhatthiBookDr[$drCounter]['touch']               = $bhatthiRow['touch'];
        $bhatthiBookDr[$drCounter]['qty']                 = $bhatthiRow['qty'];
        $bhatthiBookDr[$drCounter]['rate']                = $bhatthiRow['rate'];
        $bhatthiBookDr[$drCounter]['fine']                = $bhatthiRow['fine'];
        $bhatthiBookDr[$drCounter]['amount']              = $bhatthiRow['amount'];
        if($bhatthiBookDr[$drCounter]['weightPerc'] != 0)
        {
          $bhatthiBookDr[$drCounter]['diffWeight']         = $bhatthiBookDr[$drCounter]['grossWeight'] - $bhatthiBookDr[$drCounter]['weightPerc'];
        }
        else
        {
          $bhatthiBookDr[$drCounter]['diffWeight']         = 0; 
        }
        $drCounter++;
      }
      else
      {
        $bhatthiBookCr[$crCounter]['bhatthiBookDetailId'] = $bhatthiRow['bhatthiBookDetailId'];
        $bhatthiBookCr[$crCounter]['bhatthiBookId']  = $bhatthiRow['bhatthiBookId'];
        $bhatthiBookCr[$crCounter]['date']           = $bhatthiRow['bhatthiDate'];
        $bhatthiBookCr[$crCounter]['bhatthiPartyId'] = $bhatthiRow['bhatthiPartyId'];
        $bhatthiBookCr[$crCounter]['particulars']   = $bhatthiRow['particulars'];
        $bhatthiBookCr[$crCounter]['partyName']     = $bhatthiRow['partyName'];
        $bhatthiBookCr[$crCounter]['grossWeight']   = $bhatthiRow['grossWeight'];
        $bhatthiBookCr[$crCounter]['weightPerc']    = $bhatthiRow['weightPerc'];
        $bhatthiBookCr[$crCounter]['weight']        = $bhatthiRow['weight'];
        $bhatthiBookCr[$crCounter]['touch']         = $bhatthiRow['touch'];
        $bhatthiBookCr[$crCounter]['fine']          = $bhatthiRow['fine'];
        $bhatthiBookCr[$crCounter]['qty']           = $bhatthiRow['qty'];
        $bhatthiBookCr[$crCounter]['rate']          = $bhatthiRow['rate'];
        $bhatthiBookCr[$crCounter]['amount']        = $bhatthiRow['amount'];
        if($bhatthiBookCr[$crCounter]['grossWeight'] != 0)
        {
          $bhatthiBookCr[$crCounter]['diffWeight']    = $bhatthiBookCr[$crCounter]['weight'] - $bhatthiBookCr[$crCounter]['grossWeight'];
        }
        else
        {
          $bhatthiBookCr[$crCounter]['diffWeight']    = 0;
        }
        $crCounter++;
      }
    }
    else
    {  // Set opening : Start
      if($bhatthiRow['CrDr'] == 'Dr')
      {
        $opening['drFine']   += $bhatthiRow['fine'];
        $opening['drAmount'] += $bhatthiRow['amount'];
      }
      else
      {
        $opening['crgrossWeight'] += $bhatthiRow['grossWeight'];
        $opening['crweightPerc']  += $bhatthiRow['weightPerc'];
        $opening['crWeight']      += $bhatthiRow['weight'];
        $opening['crTouch']       += $bhatthiRow['touch'];
        $opening['crFine']        += $bhatthiRow['fine'];
        $opening['crQty']         += $bhatthiRow['qty'];
        $opening['crRate']        += $bhatthiRow['rate'];
        $opening['crAmount']      += $bhatthiRow['amount'];
      }
      $openingFinal['netFineOpening'] = $opening['drFine'] - $opening['crFine'];
	    $openingFinal['netAmountOpening'] = $opening['drAmount'] - $opening['crAmount'];
	    if($openingFinal['netFineOpening'] > 0 )
	    {
	      $openingFinalFineDr = $openingFinal['netFineOpening']; 
	      $openingFinalFineCr = "";
	    }
	    else 
	    {
	      $openingFinalFineCr = $openingFinal['netFineOpening']; 
	      $openingFinalFineDr = "";
	    }
	     if($openingFinal['netAmountOpening'] > 0 )
	    {
	      $openingFinalAmountDr = $openingFinal['netAmountOpening']; 
	      $openingFinalAmountCr = "";
	    }
	    else 
	    {
	      $openingFinalAmountCr = $openingFinal['netAmountOpening']; 
	      $openingFinalAmountDr = "";
	    }
    }// Set opening : End
    
    // this for total :Start
    if($bhatthiRow['bhatthiDate'] >= $fromDate)
    {
	    if($bhatthiRow['CrDr'] == 'Dr')
	    {
	      $total['drFine']   += $bhatthiRow['fine'];
	      $total['drQty']    += $bhatthiRow['qty'];
	      $total['drAmount'] += $bhatthiRow['amount'];
	    }
	    else
	    {
	      $total['crFine']   += $bhatthiRow['fine'];
	      $total['crQty']    += $bhatthiRow['qty'];
	      $total['crAmount'] += $bhatthiRow['amount'];
	    }
    }
    
    $totalFineCr   = 0 - $openingFinalFineCr + $total['crFine'];
    $totalAmountCr = 0 - $openingFinalAmountCr + $total['crAmount'];
    
    $totalFineDr   = $openingFinalFineDr + $total['drFine'];
    $totalAmountDr = $openingFinalAmountDr + $total['drAmount'];
    
    $total['totFine']   = $totalFineDr - $totalFineCr;
    $total['totAmount'] = $totalAmountDr - $totalAmountCr;
    
    // this for total :End
  }
}

$fromDateSelected = "";
// this for select bhatthi party : Start
$selectBhatthiParty = "SELECT bhatthiPartyId,bhatthipartyName,bhatthiFromDate 
                         FROM bhatthiparty
                        ORDER BY bhatthipartyName DESC";
$selectBhatthiPartyRes = mysql_query($selectBhatthiParty);
$a = 0; 
while($partyRow = mysql_fetch_assoc($selectBhatthiPartyRes))
{
  $party['id'][$a]              = $partyRow['bhatthiPartyId'];
  $party['name'][$a]            = $partyRow['bhatthipartyName'];
  $party['bhatthiFromDate'][$a] = $partyRow['bhatthiFromDate'];
  $a++;
  if(isset($_POST['bdate']) == "1" && $partyRow['bhatthiPartyId'] == $_POST['party'])
  {
    $fromDateSelected = $partyRow['bhatthiFromDate'];
  }
}
// this for select bhatthi party : End


$bhatthiBookCrCount  = count($bhatthiBookCr);
$bhatthiBookDrCount  = count($bhatthiBookDr);

if($bhatthiBookCrCount < $bhatthiBookDrCount)
{
	$extraBhatthiBookCr = $bhatthiBookDrCount - $bhatthiBookCrCount;
	$extraBhatthiBookDr  = 0;
}
else
{
	$extraBhatthiBookCr = 0;
	$extraBhatthiBookDr  = $bhatthiBookCrCount - $bhatthiBookDrCount; // if both are same, than this will be 0
}

$smarty = new Smarty;
require("bottom.php");
$smarty->assign("l_partySelect",$l_partySelect);
$smarty->assign("l_from",$l_from);
$smarty->assign("l_to",$l_to);
$smarty->assign("l_find",$l_find);
$smarty->assign("l_date",$l_date);
$smarty->assign("l_particulars",$l_particulars);
$smarty->assign("l_fine",$l_fine);
$smarty->assign("l_amount",$l_amount);
$smarty->assign("l_grossWeight",$l_grossWeight);
$smarty->assign("l_weightPerc",$l_weightPerc);
$smarty->assign("l_weight",$l_weight);
$smarty->assign("l_touch",$l_touch);
$smarty->assign("l_qty",$l_qty);
$smarty->assign("l_rate",$l_rate);
$smarty->assign("l_debit",$l_debit);
$smarty->assign("l_credit",$l_credit);
$smarty->assign("l_total",$l_total);
$smarty->assign("l_bhatthiDetail",$l_bhatthiDetail);
$smarty->assign("l_netFine",$l_netFine);
$smarty->assign("l_netAmount",$l_netAmount);
$smarty->assign("l_delete",$l_delete);
$smarty->assign("l_edit",$l_edit);
$smarty->assign("l_save",$l_save);

$smarty->assign("party",$party);
$smarty->assign("bhatthiBookCr",$bhatthiBookCr);
$smarty->assign("bhatthiBookDr",$bhatthiBookDr);
$smarty->assign("bhatthiPartyId",$bhatthiPartyId);
$smarty->assign("opening",$opening);
$smarty->assign("total",$total);
$smarty->assign("partyId",$partyId);
$smarty->assign("fromDate",$fromDate);
$smarty->assign("toDate",$toDate);
$smarty->assign("openingFinalFineDr",$openingFinalFineDr);
$smarty->assign("openingFinalAmountDr",$openingFinalAmountDr);
$smarty->assign("openingFinalFineCr",$openingFinalFineCr);
$smarty->assign("openingFinalAmountCr",$openingFinalAmountCr);
$smarty->assign("extraBhatthiBookCr",$extraBhatthiBookCr);
$smarty->assign("extraBhatthiBookDr",$extraBhatthiBookDr);
$smarty->assign("totalFineCr",$totalFineCr);
$smarty->assign("totalFineDr",$totalFineDr);
$smarty->assign("totalAmountCr",$totalAmountCr);
$smarty->assign("totalAmountDr",$totalAmountDr);
$smarty->assign("fromDateSelected",$fromDateSelected);
$smarty->display("bhatthiDetail.tpl");
?>